<template>
  <div class="app-container">
    <el-card class="query-card" v-show="showSearch">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
        <el-form-item label="要素编码" prop="elementCode">
          <el-input
            v-model="queryParams.elementCode"
            placeholder="请输入要素编码"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="要素名称" prop="elementName">
          <el-input
            v-model="queryParams.elementName"
            placeholder="请输入要素名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="要素类型" prop="elementType">
          <el-select v-model="queryParams.elementType" placeholder="请选择要素类型" clearable>
            <el-option
              v-for="dict in dict.type.sys_html_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="所属模块" prop="functionCode">
          <el-select v-model="queryParams.functionCode" placeholder="请选择所属模块" clearable>
            <el-option
              v-for="dict in dict.type.sys_elment_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="是否必填" prop="dvRequired">
          <el-select v-model="queryParams.dvRequired" placeholder="请选择是否必填" clearable>
            <el-option
              v-for="dict in dict.type.sys_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="是否禁填" prop="dvDisabled">
          <el-select v-model="queryParams.dvDisabled" placeholder="请选择是否禁填" clearable>
            <el-option
              v-for="dict in dict.type.sys_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="是否多选" prop="dvMultiple">
          <el-select v-model="queryParams.dvMultiple" placeholder="请选择是否多选" clearable>
            <el-option
              v-for="dict in dict.type.sys_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="启用状态" prop="elementStatus">
          <el-select v-model="queryParams.elementStatus" placeholder="请选择启用状态" clearable>
            <el-option
              v-for="dict in dict.type.sys_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
    </el-form>
    </el-card>

    <el-card class="table-card">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['system:elementConfig:add']"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['system:elementConfig:edit']"
          >修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['system:elementConfig:remove']"
          >删除</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['system:elementConfig:export']"
          >导出</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <vxe-table
        :loading="loading"
        resizable
        :row-config="{isHover: true}"
        :data="elementConfigList"
        @checkbox-all="handleSelectionChange"
        @checkbox-change="handleSelectionChange"
        :class="showSearch ? 'table-container' : 'table-container-full' "
      >
        <vxe-column type="checkbox" width="60" fixed="left" align="center"></vxe-column>
        <vxe-column title="序号" align="center" type="seq" min-width="60"/>
        <vxe-column title="要素名称" align="center" field="elementName" min-width="120" />
        <vxe-column title="要素编码" align="center" field="elementCode" min-width="120" />
        <vxe-column title="要素类型" align="center" field="elementType" min-width="120">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_html_type" :value="scope.row.elementType"/>
          </template>
        </vxe-column>
        <vxe-column title="字典编码" align="center" field="dictCode" min-width="120" />
        <vxe-column title="要素排序" align="center" field="orderNo" min-width="100" sortable/>
        <vxe-column title="所属模块" align="center" field="functionCode" min-width="150">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_elment_type" :value="scope.row.functionCode"/>
          </template>
        </vxe-column>
        <vxe-column title="是否必填" align="center" field="dvRequired" min-width="100">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.dvRequired"/>
          </template>
        </vxe-column>
        <vxe-column title="是否禁填" align="center" field="dvDisabled" min-width="100">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.dvDisabled"/>
          </template>
        </vxe-column>
        <vxe-column title="是否多选" align="center" field="dvMultiple" min-width="100">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.dvMultiple"/>
          </template>
        </vxe-column>
        <vxe-column title="启用状态" align="center" field="elementStatus" min-width="100">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.elementStatus"/>
          </template>
        </vxe-column>
        <vxe-column title="备注" align="center" field="remark" min-width="150"/>
        <vxe-column title="操作" align="center" min-width="120" fixed="right">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['system:elementConfig:edit']"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['system:elementConfig:remove']"
            >删除</el-button>
          </template>
        </vxe-column>
      </vxe-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>

    <!-- 添加或修改要素配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" v-if="open" v-drag-zoom append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row class="row-content">
          <el-col :span="12">
            <el-form-item label="要素名称" prop="elementName">
              <el-input v-model="form.elementName" placeholder="请输入要素名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="要素编码" prop="elementCode">
              <el-input v-model="form.elementCode" placeholder="请输入要素编码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="要素类型" prop="elementType">
              <el-select v-model="form.elementType" @change="handleElementTypeChange" placeholder="请选择要素类型" clearable>
                <el-option
                  v-for="dict in dict.type.sys_html_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属模块" prop="functionCode">
              <el-select v-model="form.functionCode" placeholder="请选择所属模块" clearable>
                <el-option
                  v-for="dict in dict.type.sys_elment_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="数据字典"
              prop="dictCode"
              :rules="['select', 'checkbox', 'radio'].includes(form.elementType) ? [{ required: true, message: '字典编码不能为空', trigger: 'change' }] : []">
              <el-select v-model="form.dictCode" :disabled="!['select', 'checkbox', 'radio'].includes(form.elementType)" clearable filterable placeholder="请选择">
                <el-option
                  v-for="dict in dictOptions"
                  :key="dict.dictType"
                  :label="dict.dictName"
                  :value="dict.dictType">
                  <span style="float: left">{{ dict.dictName }}</span>
                  <span style="float: right; color: #8492a6; font-size: 13px">{{ dict.dictType }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否必填" prop="dvRequired">
              <el-select v-model="form.dvRequired" placeholder="请选择是否必填" clearable>
                <el-option
                  v-for="dict in dict.type.sys_yes_no"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否禁填" prop="dvDisabled">
              <el-select v-model="form.dvDisabled" placeholder="请选择是否禁填" clearable>
                <el-option
                  v-for="dict in dict.type.sys_yes_no"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否多选" prop="dvMultiple">
              <el-select v-model="form.dvMultiple" :disabled="form.elementType !== 'select'" placeholder="请先选择要素类型为下拉框" clearable>
                <el-option
                  v-for="dict in dict.type.sys_yes_no"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="排序序号" prop="orderNo">
              <el-input-number v-model="form.orderNo" :min="-20" :max="10000" label="请输入排序序号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="启用状态">
              <el-radio-group v-model="form.elementStatus">
                <el-radio
                  v-for="dict in dict.type.sys_yes_no"
                  :key="dict.value"
                  :label="dict.value"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listElementConfig, getElementConfig, delElementConfig, addElementConfig, updateElementConfig } from "@/api/system/elementConfig";
import { optionselect as getDictOptionselect } from '@/api/system/dict/type'

export default {
  name: "ElementConfig",
  dicts: ['sys_yes_no', 'sys_html_type', 'sys_elment_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 要素配置表格数据
      elementConfigList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        elementCode: null,
        elementName: null,
        elementType: null,
        dictCode: null,
        functionCode: null,
        dvRequired: null,
        dvDisabled: null,
        dvMultiple: null,
        elementStatus: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        elementCode: [
          { required: true, message: "要素编码不能为空", trigger: "blur" }
        ],
        elementName: [
          { required: true, message: "要素名称不能为空", trigger: "blur" }
        ],
        elementType: [
          { required: true, message: "要素类型不能为空", trigger: "change" }
        ],
        functionCode: [
          { required: true, message: "所属模块不能为空", trigger: "change" }
        ],
      },
      // 字典信息
      dictOptions: [],
    };
  },
  created() {
    this.getList();
    this.queryDictList();
  },
  methods: {
    /** 查询要素配置列表 */
    getList() {
      this.loading = true;
      listElementConfig(this.queryParams).then(response => {
        this.elementConfigList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询字典下拉列表 */
    queryDictList() {
      getDictOptionselect().then(response => {
        this.dictOptions = response.data;
      });
    },
    /** 要素类型变更 */
    handleElementTypeChange() {
      if (!['select', 'checkbox', 'radio'].includes(this.form.elementType)) {
        this.form.dictCode = '';
        this.form.dvMultiple = '';
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        elementCode: null,
        elementName: null,
        elementType: null,
        dictCode: null,
        functionCode: null,
        dvRequired: null,
        dvDisabled: null,
        dvMultiple: null,
        orderNo: null,
        elementStatus: "0",
        createBy: null,
        updateBy: null,
        remark: null,
        delFlag: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(row) {
      this.ids = row.records.map(item => item.id)
      this.single = row.records.length!==1
      this.multiple = !row.records.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加要素配置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getElementConfig(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改要素配置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateElementConfig(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addElementConfig(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除要素配置编号为"' + ids + '"的数据项？').then(function() {
        return delElementConfig(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/elementConfig/export', {
        ...this.queryParams
      }, `elementConfig_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style lang="scss" scoped>
::v-deep .table-container {
  .vxe-table {
    height: calc(100vh - 364.5px);
  }
  .vxe-table--body-wrapper {
    height: calc(100vh - 408.5px);
  }
  .fixed-left--wrapper,
  .fixed-right--wrapper {
    height: calc(100vh - 417.5px);
  }
}
</style>
